package org.example.book.controller;

import jakarta.servlet.http.HttpSession;
import org.example.book.constant.Constants;
import org.example.book.model.UserInfo;
import org.example.book.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;
    @RequestMapping("/login")
    public Boolean login(String userName, String password, HttpSession session) {
       //校验参数合法性
        if(!StringUtils.hasLength(userName)||!StringUtils.hasLength(password)) {
            return false;
        }
        UserInfo userInfo = userService.getUserInfoByName(userName);
        if(userInfo==null||userInfo.getId()<=0) {
            return false;
        }
        if(password.equals(userInfo.getPassword())) {
            userInfo.setPassword(null);
            session.setAttribute(Constants.SESSION_USER_KEY, userInfo);
            return true;
        }

        return false;
    }
}
